//index.js
//获取应用实例
const app = getApp()
var bannerTime;
Page({
  data: {
    bannerList:[{
      thumb:"../img/bg_1.png"
    }, {
        thumb: "../img/bg_2.png"
      }, {
        thumb: "../img/bg_3.png"
      }],
    bannerIndex: 1,
    bannerMiddle: 0,
    bannerChange: 0,
    info:"no msg"，
    move:"right",
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this;
    var singList = that.selectComponent("#load");
    singList.fun(true);
    //本地存放信息
    wx.setStorage({
      key: "info",
      data: 'i am from storage'
    })
    setTimeout(function(){
      singList.fun(false);
      //本地取出信息
      var msg = wx.getStorageSync("info");
      that.setData({
        info:msg
      })
    },3000)
    bannerTime = setInterval(function () {
      var bannerI = parseInt(that.data.bannerIndex);
      that.setData({
        bannerMiddle: bannerI
      })
      var banLen = that.data.bannerList.length;
      bannerI = bannerI + 1;
      if (bannerI > banLen) {
        bannerI = 1
      }
      that.setData({
        bannerIndex: bannerI
      })
    }, 5000)
  },
  //用户滑动切换轮播
  touchIndex(e) {
    var cur = e.touches[0].pageX
    this.setData({
      bannerChange: cur
    })
  },
  touchIndexEnd(e) {
    var cur = e.changedTouches[0].pageX;
    var position = this.data.bannerChange - cur;
    var i = this.data.bannerIndex;
    var banLen = this.data.bannerList.length;
    var that = this;
    if (bannerTime) {
      clearInterval(bannerTime)
    }
    bannerTime = setInterval(function () {
      var bannerI = parseInt(that.data.bannerIndex);
      that.setData({
        bannerMiddle: bannerI
      })
      var banLen = that.data.bannerList.length;
      bannerI = bannerI + 1;
      if (bannerI > banLen) { bannerI = 1 }
      that.setData({
        bannerIndex: bannerI,
        move: 'right'
      })
    }, 5000)
    if (position < -100) {
      this.setData({
        move: 'left'
      })
      var a = i
      i = i - 1;
      if (i == 0) { i = banLen; }
      this.setData({
        bannerIndex: i,
      })
    } else if (position > 100) {
      this.setData({
        move: 'right'
      })
      i = i + 1;
      if (i > banLen) { i = 1; }
      this.setData({
        bannerIndex: i,
      })
    }
  },

})
